<?php

namespace app\admin\controller;

use think\Request;
use think\Validate;

use kdcs\Spider;

class Yizhandata extends Base
{
    public function index()
    {
        return view('get_data');
    }

    public function getData(){
        if(!cache('kdcs_session')){
            return json(msg(-2, '', '请先登录'));
        }
        $page = input('post.page', 1);
        $cookie = cache('kdcs_session');
        if($cookie){
            $spider = new Spider();
            $res = $spider->getdata($cookie, $page);
            //获取curl状态
            if($res['code'] && $res['http_code'] == 200){
                $data = json_decode($res['res'], true);
                //获取快递超市返回数据状态
                if($data['status']){
                    $msg = '未完全获取';
                    $tag = 0;   //完全获取的标志
                    $count = $spider->importDatabase($data['result']['items']);
                    //获取中总数量
                    if(!session('?pageCount')){
                        $num = intval($data['result']['recordCount']/100) + 1;
                        session('pageCount', $num);
                    }
                    if($page == session('pageCount')){//获取完毕
                        session('pageCount', null);
                        $tag = 1;
                    }
                    return json(['code'=>1, 'data'=>$count, 'msg'=>$msg, 'complete'=>$tag]);
                }else{
                    return json(msg(-2, '', $res));
                }
            }else{
                return json(msg(-1, '', $res['error']));
            }
        }
    }

    public function getSession(){
        $spider = new Spider;
        $cookie = $spider->getSession();
        $this->getCode($cookie);
        if($cookie){
            cache('kdcs_session', $cookie, 3600);
            return json(msg(0, $cookie,'ok'));
        }else{
            return json(msg(-1, '', '获取session失败'));
        }
    }

    public function getCode() {
        if(!cache('kdcs_session')){
            return json(msg(-2, '', '请先登录'));
        }
        $spider = new Spider;
        $img = $spider->getCode(cache('kdcs_session'));
        if($img){
            return json(msg(0, '', 'ok'));
        }else{
            return json(msg(-1, '', '获取验证码失败'));
        }
    }

    public function login(){
        if(!cache('kdcs_session')){
            return json(msg(-2, '', '请先登录'));
        }
        $info = input('post.');
        $validate = Validate::make([
            'username|用户名'   => 'require',
            'password|密码'     => 'require',
            'code|验证码'        => 'require',
        ]);
        if(!$validate->check($info)){
            return json(msg(-1, '', $validate->getError()));
        }
        $spider = new Spider;
        $res = $spider->doLogin($info['username'], $info['password'], $info['code'], cache('kdcs_session'));
        // dump($res);
        $body = json_decode($res['body']);
        if($body->status === true && $body->statusCode == 'SYS000'){
            $rex = '/Set-Cookie: .*;/';
            if(preg_match($rex, $res['head'], $matches)){
                $sessionStr = substr($matches[0], 12, 44);
                cache('kdcs_session', $sessionStr);
                return msg(1, '', '登录成功');
            }
        }else if($body->status == 500){
            $rex = '/JSESSIONID.{33}/';
            if(preg_match($rex, $res['head'], $matches)){
                $session = cache('kdcs_session');
                cache('kdcs_session', $session .'; '. $matches[0]);
                return msg(-2, $body, '登录错误');
            }
        }else{
            if(cache('kdcs_session')){
                return json(msg(-2, '', '登录错误，请重试'));
            }
            return json(msg(-1, '', 'fail'));
        }
    }
}
